﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.BaseCode
{
    public class CommMethod
    {
        /// <summary>
        /// 获取前台分页代码
        /// </summary>
        /// <param name="pageListType">列表页类型</param>
        /// <param name="ename">类别英文名称(没有可不写，如：网站首页)</param>
        /// <param name="count">总页数</param>
        /// <param name="curPageIndex">当前页</param>
        /// <returns></returns>
        public static string GetPaging(BlogPageType pageType, int count, int curPageIndex = 0, string ename = "")
        {
            //&laquo;&raquo;&hellip;
            string prev = "<a class=\"prev page-numbers\" href=\"{0}\">&laquo;</a>";
            string numStr = "<a class='page-numbers' href='{0}'>{1}</a>";
            string cur = "<span class='page-numbers current'>{0}</span>";
            string lue = "<span class=\"page-numbers dots\">&hellip;</span>";
            string next = "<a class=\"next page-numbers\" href=\"{0}\">&raquo;</a>";

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (curPageIndex != 1)
                sb.Append(string.Format(prev, GetUrl(pageType, ename, curPageIndex - 1)));        //上一页

            if (curPageIndex != 1)
                sb.Append(string.Format(numStr, GetUrl(pageType, ename, 1), 1)); //第一页

            if ((curPageIndex - 2) > 2 && (curPageIndex - 2) < count)
                sb.Append(lue); //省略号

            if ((curPageIndex - 2) > 1 && (curPageIndex - 2) < count)
                sb.Append(string.Format(numStr, GetUrl(pageType, ename, curPageIndex - 2), curPageIndex - 2));   //当前页的前一页

            if ((curPageIndex - 1) > 1 && (curPageIndex - 1) < count)
                sb.Append(string.Format(numStr, GetUrl(pageType, ename, curPageIndex - 1), curPageIndex - 1));   //当前页向前第二页

            //if (curPageIndex > 0)
            sb.Append(string.Format(cur, curPageIndex));    //当前页

            if ((curPageIndex + 1) < count)
                sb.Append(string.Format(numStr, GetUrl(pageType, ename, curPageIndex + 1), curPageIndex + 1)); //当前页的下一页

            if ((curPageIndex + 2) < count)
                sb.Append(string.Format(numStr, GetUrl(pageType, ename, curPageIndex + 2), curPageIndex + 2)); //当前页向后的第二页

            if ((curPageIndex + 2) < count)
                sb.Append(lue);

            if (count > curPageIndex)
                sb.Append(string.Format(numStr, GetUrl(pageType, ename, count), count));    //最后一页

            if (count != curPageIndex)
                sb.Append(string.Format(next, GetUrl(pageType, ename, curPageIndex + 1))); //下一页

            return sb.ToString();
        }

        /// <summary>
        /// 获取统一编码的url地址
        /// </summary>
        /// <param name="blogUrlType"></param>
        /// <param name="ename"></param>
        /// <param name="curPageIndex"></param>
        /// <returns></returns>
        public static string GetUrl(BlogPageType blogUrlType, string ename = "", int curPageIndex = 0)
        {
            string domain = BaseCode.CommValue.Domain;

            string url = string.Empty;
            switch (blogUrlType)
            {
                case BlogPageType.Article:
                    {
                        url = string.Format("{0}/article/{1}.html", domain, ename);
                        break;
                    }
                case BlogPageType.Home:
                    {
                        if (curPageIndex == 0 || curPageIndex == 1)
                        {
                            url = string.Format("{0}/index.html", domain);
                        }
                        else
                        {
                            url = string.Format("{0}/index_{1}.html", domain, curPageIndex);
                        }
                        break;
                    }
                case BlogPageType.Item:
                    {
                        if (curPageIndex == 0 || curPageIndex == 1)
                        {
                            url = string.Format("{0}/items/{1}.html", domain, ename);
                        }
                        else
                        {
                            url = string.Format("{2}/items/{0}_{1}.html", ename, curPageIndex, domain);
                        }
                        break;
                    }
                case BlogPageType.Category:
                    {
                        if (curPageIndex == 0 || curPageIndex == 1)
                        {
                            url = string.Format("{1}/category/{0}.html", ename, domain);
                        }
                        else
                        {
                            url = string.Format("{2}/category/{0}_{1}.html", ename, curPageIndex, domain);
                        }
                        break;
                    }
                case BlogPageType.Tag:
                    {
                        if (curPageIndex == 0 || curPageIndex == 1)
                        {
                            url = string.Format("{1}/tags/{0}.html", ename, domain);
                        }
                        else
                        {
                            url = string.Format("{2}/tags/{0}_{1}.html", ename, curPageIndex, domain);
                        }
                        break;
                    }
                case BlogPageType.Search:
                    {
                        if (curPageIndex == 0 || curPageIndex == 1)
                        {
                            url = string.Format("{1}/search/{0}.html", ename, domain);
                        }
                        else
                        {
                            url = string.Format("{2}/search/{0}_{1}.html", ename, curPageIndex, domain);
                        }
                        break;
                    }

                default:
                    url = domain;
                    break;
            }
            return url;
        }
    }
}